﻿using UnityEngine;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Hont.C2UTool;


public class C4DVetexAnimator : MonoBehaviour
{
    public struct MappingVetexInfo
    {
        public Transform TransformInfo;
        public int[] VetexIDArr;
    }
    public Transform[] vertexArray;
    public MeshFilter mesh;

    MappingVetexInfo[] mMappingVetexInfoArr;


    void Start()
    {
        mMappingVetexInfoArr = new MappingVetexInfo[vertexArray.Length];
        var vertexsList = mesh.mesh.vertices.ToList();

        for (int i = 0; i < vertexArray.Length; i++)
        {
            var item = vertexArray[i];

            mMappingVetexInfoArr[i] = new MappingVetexInfo();
            mMappingVetexInfoArr[i].TransformInfo = item;


            List<int> mIndexList = new List<int>();

            for (int j = 0; j < vertexsList.Count; j++)
            {
                var vertex = vertexsList[j];

                if (transform.TransformPoint(vertex) == item.position)
                    mIndexList.Add(j);
            }

            mMappingVetexInfoArr[i].VetexIDArr = mIndexList.ToArray();
        };
    }

    void Update()
    {
        var vertexs = mesh.mesh.vertices;

        for (int i = 0; i < mMappingVetexInfoArr.Length; i++)
        {
            var item = mMappingVetexInfoArr[i];

            for (int j = 0; j < item.VetexIDArr.Length; j++)
            {
                var vertexID = item.VetexIDArr[j];

                vertexs[vertexID] = transform.InverseTransformPoint(item.TransformInfo.position);
            }
        }

        mesh.mesh.vertices = vertexs;
    }
}
